<?php
/* $Id: register.php 14 2009-04-11 17:40:34Z ronan $ */
error_reporting(-1);
$basesite=$_SERVER['PHP_SELF'];
require("common.php");
IGBAuthorize();
$link = mysql_connect($host,$dbuser,$dbpassword)
    OR die(mysql_error());
if(!$isTGRAD) quit("Registration is restricted to TGRAD members.");
$results = Query("SELECT * FROM users WHERE name = '".mysql_real_escape_string($pilotname)."'");
$page = mysql_fetch_assoc($results);
//print_r ($page);
if ($page['name'] == $pilotname) quit ("It appears that you are already registered.</a>");
if(isset($_POST['password'])) $pass = $_POST['password'];
if(isset($_POST['cpassword'])) $cpass = $_POST['cpassword'];
if(isset($_POST['email'])) $email = $_POST['email'];
if(isset($pass)&&isset($cpass)&&isset($email)) { //Form was submitted
	$filledout = true;
	if($pass==$cpass) $passconfirmed = true;
}
if(isset($email)) if(preg_match($emailRegex, $email)) $goodemail = true;
if(isset($passconfirmed)) if(is_alphanumeric($pass)) $goodpass=true;
require("header.php");
if(isset($filledout) && isset($passconfirmed) && isset($goodemail) && isset($goodpass)) {
	//echo "DEBUG: using query: " ."INSERT INTO users(name, password, email, userLevel) VALUES ('".str_replace("'", "\\'", $pilotname)."', PASSWORD('$pass'), '$email', 'user')"; 
	Query("INSERT INTO users(name, password, email, userLevel) VALUES ('".str_replace("'", "\\'", $pilotname)."', PASSWORD('$pass'), '$email', 'user')");
	echo "User successfully added. <a href=\"index.php\">Return home</a>.";	
} else {
	echo "Note that your password does NOT have to be (and really SHOULD NOT be) the same as your EVE password.<br /><br />\n\n";
	echo "<form method=\"post\" action=\"register.php\">Name: $pilotname <br />\n" .
		"Corp: $pilotcorp <br />\n";
	if(isset($cpass) && isset($pass) && !isset($goodpass)) echo "<font color=\"red\">PASSWORDS MUST BE ALPHANUMERIC</font> - ";
	echo "Tool password: <input type=\"password\" name=\"password\" /> <br />\n";
	if(isset($cpass) && isset($pass) && $pass!=$cpass) echo "<font color=\"red\">PASSWORDS DO NOT MATCH</font> - ";
	echo "Confirm password: <input type=\"password\" name=\"cpassword\" /> <br />\n";
	if(isset($email) && !isset($goodemail)) echo "<font color=\"red\">INVALID EMAIL</font> - ";
	echo "Email: <input type=\"text\" name=\"email\" /> (used only for password retrieval)<br />\n" .
		"<input type=\"submit\" value=\"Register\"></form>"; 
}
require("footer.php");
?>
